package cc.chenwenxi.mypi.appium;

import cc.chenwenxi.mypi.appium.kits.AppiumDriverWait;
import cc.chenwenxi.mypi.appium.kits.AppiumUtils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
public class 抖音极速版AppiumTask implements AppiumTask {
    private static final Activity mainActivity = new Activity("com.ss.android.ugc.aweme.lite", "com.ss.android.ugc.aweme.splash.SplashActivity");

    private final AndroidDriver driver;
    private final AppiumUtils kit;

    private final String pre = "抖音极速版_";

    // --------------------------------导航--------------------------
    @AndroidFindBy(xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout[1]/dmt.viewpager.DmtViewPager.c/android.widget.TabHost/android.widget.FrameLayout/android.widget.FrameLayout[3]/android.widget.LinearLayout/android.widget.FrameLayout[1]")
    private AndroidElement 首页导航;
    @AndroidFindBy(xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout[1]/dmt.viewpager.DmtViewPager.c/android.widget.TabHost/android.widget.FrameLayout/android.widget.FrameLayout[3]/android.widget.LinearLayout/android.widget.FrameLayout[4]")
    private AndroidElement 我的导航;
    @AndroidFindBy(id = "com.ss.android.ugc.aweme.lite:id/byf")
    private AndroidElement 来赚钱导航;


    // --------------------------------首页页面--------------------------
    @AndroidFindBy(id = "com.ss.android.ugc.aweme.lite:id/ai2")
    private AndroidElement 点赞;
    @AndroidFindBy(id = "com.ss.android.ugc.aweme.lite:id/a_l")
    private AndroidElement 评论;

    // --------------------------------来赚钱页面--------------------------
    @AndroidFindBy(xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[1]/android.view.View/android.view.View[4]/android.view.View")
    private AndroidElement 宝箱;
    @AndroidFindBy(xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View[2]/android.view.View[2]/android.view.View[3]/android.view.View")
    private AndroidElement 点击宝箱后_看广告再赚XX金币;
    @AndroidFindBy(xpath = "//android.view.ViewGroup[@content-desc=\"关闭当前视频\"]")
    private AndroidElement 点击宝箱后_看广告再赚XX金币_关闭;

    @AndroidFindBy(uiAutomator = """
               new UiSelector().textMatches("去领取")
            """)
    private AndroidElement 来赚钱_看广告赚金币;

    public 抖音极速版AppiumTask(AndroidDriver driver) {
        this.driver = driver;
        this.kit = new AppiumUtils(driver);

        ThreadUtil.execute(() -> this.关闭弹窗1());
        ThreadUtil.execute(() -> this.关闭弹窗2());
        ThreadUtil.execute(() -> this.关闭弹窗3());

        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

    @Override
    public String appName() {
        return "抖音极速版";
    }

    @Getter
    @AllArgsConstructor
    public static class 弹窗对象 {
        private WebElement elementByAndroidUIAutomator;
        private String info;
    }

    @Override
    public void run(Date endTime) {
        while (true) {
            try {
                if (endTime.before(new Date())) {
                    log.info("运行结束!!,过期时间为:{}", DateUtil.formatDateTime(endTime));
                    break;
                }
                this.main();
            } catch (Exception e) {
                log.error("异常,跳出本循环", e);
                break;
            } finally {
                int i = RandomUtil.randomInt(1, 3);
                log.info("间隔:{}", i);
                ThreadUtil.sleep(i, TimeUnit.SECONDS);
            }
        }
    }

    private void 关闭弹窗1() {
        try {
            WebElement 弹窗 = new AppiumDriverWait(driver, 3600).untilMy(driver, (AndroidElement)driver.findElementByAndroidUIAutomator("new UiSelector().textMatches(\"我知道了\")") );
            弹窗.click();
            log.info("关闭----弹窗------提示青少年成长_弹窗");
        } catch (Exception e) {

        }
    }
    private void 关闭弹窗2() {
        try {
            WebElement 弹窗 = new AppiumDriverWait(driver, 3600).untilMy(driver, (AndroidElement)driver.findElementById("com.ss.android.ugc.aweme.lite:id/bai") );
            弹窗.click();
            log.info("关闭----弹窗------发给你一个视频红包");
        } catch (Exception e) {

        }
    }
    private void 关闭弹窗3() {
        try {
            WebElement 弹窗 = new AppiumDriverWait(driver, 3600).untilMy(driver, (AndroidElement)driver.findElementByAndroidUIAutomator("new UiSelector().textMatches(\"以后再说\")") );
            弹窗.click();
            log.info("关闭----弹窗------升级提醒");
        } catch (Exception e) {

        }
    }

    public void main() {
        int minute = DateUtil.minute(DateUtil.date());
        if (minute % 10 == 0 || true) {
            log.info("点击_导航_来赚钱");
            来赚钱导航.click();
            // 点击立即领取金币按钮
            try {
                WebElement 立即领取 = new AppiumDriverWait(driver, 15).untilMy(driver, (AndroidElement) driver.findElementByAndroidUIAutomator("new UiSelector().textMatches(\"立即领取\")"));
                立即领取.click();
                log.info("领取当天签到金币");
            } catch (Exception e) {
                log.info("没有找到领取当天签到金币");
            }
            this.开宝箱看视频广告();
        } else {
            if (!首页导航.isSelected()) {
                log.info("点击_首页导航");
                首页导航.click();
            }
            kit.swipeUp();
            log.info("{}上滑", pre);
            if (RandomUtil.randomBoolean()) {
                log.info("点赞");
                kit.click(点赞, 0, 0);
            } else {
                log.info("不点赞");
            }
        }
    }

    private void 开宝箱看视频广告() {
        log.info("宝箱信息:{}", 宝箱.getText());
        if (!"开宝箱得金币".equals(宝箱.getText())) {
            log.warn("不需要打开宝箱,返回!!");
            driver.navigate().back();
            return;
        }
        log.info("点击_宝箱");
        宝箱.click();
        ThreadUtil.sleep(2, TimeUnit.SECONDS);
        log.info("点击_宝箱后_看广告再赚XX金币,暂停65s");
        点击宝箱后_看广告再赚XX金币.click();
        ThreadUtil.sleep(65, TimeUnit.SECONDS);
        log.info("点击关闭广告");
        kit.click(点击宝箱后_看广告再赚XX金币_关闭, 0, 0);
    }

    @Override
    public void openActivity() {
        driver.startActivity(mainActivity);
    }

    @Override
    public void checkAndInstallApk() {

    }

    @Override
    public boolean isNeedRun() {
        return true;
    }
}
